layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var DT = {
        tableId: "dataTable",    //表格id
        condition: {
            name: ""
        }
    };

    /**
     * 初始化表格的列
     */
    DT.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: '租户id'},
            {field: 'code', sort: false, title: '租户编号'},
            {field: 'name', sort: false, title: '租户名称'},
            {field: 'projects', sort: false, title: '管理的项目', templet: function(data){
                     var projs='';
                        for ( var r in data.projects) {
                            projs= projs+( '<i class="layui-icon layui-icon-flag"></i>'+data.projects[r]+"<br>");
                        }
                        return projs;
                    }
            },
            {field: 'status',align: 'center', sort: false, templet: function(data){

                        return data.status==1?'<span class="layui-badge layui-bg-green">启用</span>':'<span class="layui-badge layui-bg-orange">禁用</span>';
                    }
                    ,title: '状态',minWidth: 80},
            {field: 'createTime', sort: false,title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    DT.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(DT.tableId, {where: queryData});
    };

    DT.openAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: ['650px','520px'],
            title: '新增',
            content: Sam.ctxPath + '/sam/lessee/add',
            end: function () {
                admin.getTempData('formOk') && DT.search();
            }
        });
    };

    DT.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: ['650px','520px'],
            title: '修改',
            content: Sam.ctxPath + '/sam/lessee/edit/' + data.id,
            end: function () {
                admin.getTempData('formOk') && DT.search();
            }
        });
    };

    DT.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Sam.ctxPath + "/sam/lessee/delete/"+data.id, function () {
                Sam.success("删除成功!");
                DT.search();
            }, function (data) {
                Sam.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.start();
        };
        Sam.confirm("是否删除[" + data.name + "]?", operation);
    };

    var tableResult = table.render({
        elem: '#' + DT.tableId,
        url: Sam.ctxPath + '/sam/lessee/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: DT.initColumn()
    });

    $('#btnSearch').click(function () {
        DT.search();
    });

    $('#btnAdd').click(function () {
        DT.openAdd();
    });


    table.on('tool(' + DT.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DT.onEdit(data);
        } else if (layEvent === 'delete') {
            DT.onDelete(data);
        }

    });

});
